package com.yss.kettle.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Date;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yss.common.core.enums.YssStatusEnum;
import com.yss.common.core.utils.easyexecl.EasyExeclUtil;
import com.yss.common.core.utils.uuid.IdUtils;
import com.yss.common.security.utils.SecurityUtils;
import com.yss.kettle.domain.JobDatasource;
import com.yss.kettle.domain.JobParam;
import com.yss.kettle.service.IJobDatasourceService;
import com.yss.kettle.service.IJobParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.kettle.domain.JobTaskDetails;
import com.yss.kettle.service.IJobTaskDetailsService;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.core.web.page.TableDataInfo;

/**
 * 作业任务明细信息Controller
 *
 * @author yss
 * @date 2023-04-23
 */
@RestController
@RequestMapping("/details")
public class JobTaskDetailsController extends BaseController {
    @Autowired
    private IJobTaskDetailsService jobTaskDetailsService;
    @Autowired
    private IJobDatasourceService jobDatasourceService;

    /**
     * 查询作业任务明细信息列表
     */
    @RequiresPermissions("kettle:details:list")
    @GetMapping("/list")
    public TableDataInfo list(JobTaskDetails jobTaskDetails) {
        startPage();
        List<JobTaskDetails> list = jobTaskDetailsService.selectJobTaskDetailsList(jobTaskDetails);
        return getDataTable(list);
    }

    /**
     * 导出作业任务明细信息列表
     */
    @RequiresPermissions("kettle:details:export")
    @Log(title = "作业任务明细信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, JobTaskDetails jobTaskDetails) {
        List<JobTaskDetails> list = jobTaskDetailsService.selectJobTaskDetailsList(jobTaskDetails);
        EasyExeclUtil.export(response, list, JobTaskDetails.class);
    }

    /**
     * 初始化 加载
     */
    @RequiresPermissions("kettle:detail:list")
    @Log(title = "作业任务明细信息", businessType = BusinessType.OTHER)
    @PutMapping("/init")
    public AjaxResult init() {
        AjaxResult ajax = AjaxResult.success();
        QueryWrapper<JobDatasource> wrapper = new QueryWrapper<>();
        wrapper.eq("STATUS", YssStatusEnum.NORMAL.getCode());
        ajax.put("jobDatasource", jobDatasourceService.list(wrapper));
        return ajax;
    }

    /**
     * 获取作业任务明细信息详细信息
     */
    @RequiresPermissions("kettle:details:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(jobTaskDetailsService.getById(id));
    }

    /**
     * 新增作业任务明细信息
     */
    @RequiresPermissions("kettle:details:add")
    @Log(title = "作业任务明细信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated JobTaskDetails jobTaskDetails) {
        jobTaskDetails.setId(IdUtils.fastSimpleUUID());
        jobTaskDetails.setCreateBy(SecurityUtils.getUsername());
        jobTaskDetails.setCreateTime(new Date());
        return toAjax(jobTaskDetailsService.save(jobTaskDetails));
    }

    /**
     * 修改作业任务明细信息
     */
    @RequiresPermissions("kettle:details:edit")
    @Log(title = "作业任务明细信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated JobTaskDetails jobTaskDetails) {
        jobTaskDetails.setUpdateTime(new Date());
        jobTaskDetails.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(jobTaskDetailsService.updateById(jobTaskDetails));
    }

    /**
     * 删除作业任务明细信息
     */
    @RequiresPermissions("kettle:details:remove")
    @Log(title = "作业任务明细信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids) {
        return toAjax(jobTaskDetailsService.removeByIds(ids));
    }
    /**
     * 保存表顺序信息
     */
    @RequiresPermissions("reportworld:details:orderNum" )
    @Log(title = "表明细作业任务明细信息信息" , businessType = BusinessType.UPDATE)
    @PostMapping("/handleUpdateOrderNum" )
    public AjaxResult handleUpdateOrderNum(@RequestBody String josn) {
        List<JobTaskDetails> list = JSONArray.parseArray(josn, JobTaskDetails.class);
        return toAjax(jobTaskDetailsService.updateBatchById(list));
    }
}
